﻿using System;
using System.Windows.Forms;

namespace CalcConvertor
{
    public enum ConvertSource { None, Bin, Oct, Dec, Hex };
    public partial class FormCalcConvertor : Form
    {
        public FormCalcConvertor()
        {
            InitializeComponent();
        }
        
        

        public static ConvertSource convertSource = ConvertSource.None;
        
        private void CalcConvertor_Load(object sender, EventArgs e)
        {
            txtBox_Click(txtBin, null);
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
           

            switch (convertSource)
            {
                case ConvertSource.Bin:
                    BeginConvert(txtBin);
                    break;
                case ConvertSource.Oct:
                    BeginConvert(txtOct);
                    break;
                case ConvertSource.Dec:
                    BeginConvert(txtDec);
                    break;
                case ConvertSource.Hex:
                    BeginConvert(txtHex);
                    break;
                case ConvertSource.None:
                default: 
                    break;
            }
            


        }

        private void BeginConvert(TextBox textBox)
        {
            try
            {
                if (textBox.Text.IndexOf('-') > -1)
                {
                    MessageBox.Show("sorry,it does not support signed number.");
                    return;
                }
                switch (convertSource)
                {
                    case ConvertSource.Bin:
                        txtDec.Text = MathTool.BinStrToDecStr(textBox.Text.Trim());
                        txtOct.Text = MathTool.DecStrToOctStr(txtDec.Text);
                        txtHex.Text = MathTool.DecstrToHexStr(txtDec.Text);
                        break;
                    case ConvertSource.Oct:
                        txtDec.Text = MathTool.OctStrToDecStr(textBox.Text.Trim());
                        txtBin.Text = MathTool.DecStrToBinStr(txtDec.Text);
                        txtHex.Text = MathTool.DecstrToHexStr(txtDec.Text);
                        break;
                    case ConvertSource.Dec:
                        txtBin.Text = MathTool.DecStrToBinStr(textBox.Text.Trim());
                        txtOct.Text = MathTool.DecStrToOctStr(textBox.Text.Trim());
                        txtHex.Text = MathTool.DecstrToHexStr(textBox.Text.Trim());
                        break;
                    case ConvertSource.Hex:
                        txtDec.Text = MathTool.HexStrToDecStr(textBox.Text.Trim());
                        txtBin.Text = MathTool.DecStrToBinStr(txtDec.Text);
                        txtOct.Text = MathTool.DecStrToOctStr(txtDec.Text);
                        break;
                    case ConvertSource.None:
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message );
            }
        }

        private void txtBox_Click(object sender, EventArgs e)
        {
            
           
            TextBox textBox = sender as TextBox;
            
            if (textBox == null)
                throw new NullReferenceException();

            if (textBox == txtBin)
            {
                convertSource = ConvertSource.Bin;
                txtBin.BorderStyle = BorderStyle.FixedSingle;
                
                
                txtDec.BorderStyle = txtHex.BorderStyle = txtOct.BorderStyle = BorderStyle.Fixed3D;
               
            }
            else if (textBox == txtOct)
            {
                convertSource = ConvertSource.Oct;
                txtOct.BorderStyle = BorderStyle.FixedSingle;
                
                txtBin.BorderStyle = txtDec.BorderStyle = txtHex.BorderStyle = BorderStyle.Fixed3D;
                
            }
            else if (textBox == txtDec)
            {
                convertSource = ConvertSource.Dec;
                txtDec.BorderStyle = BorderStyle.FixedSingle;

                txtBin.BorderStyle = txtHex.BorderStyle = txtOct.BorderStyle = BorderStyle.Fixed3D;
                
            }
            else if (textBox == txtHex)
            {
                convertSource = ConvertSource.Hex;
                txtHex.BorderStyle = BorderStyle.FixedSingle;

                txtBin.BorderStyle = txtDec.BorderStyle = txtOct.BorderStyle = BorderStyle.Fixed3D;
             
            }
            else
            {
                convertSource = ConvertSource.None;
                txtBin.BorderStyle =txtBin.BorderStyle = txtDec.BorderStyle = txtOct.BorderStyle = BorderStyle.Fixed3D;
            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            convertSource = ConvertSource.None;
            txtBin.BorderStyle = txtBin.BorderStyle = txtDec.BorderStyle = txtOct.BorderStyle = BorderStyle.Fixed3D;
            txtBin.Text = txtDec.Text = txtHex.Text = txtOct.Text = "";
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This is a small tool to \nconvert numbers with\n different number system.");
        }

        
        
    }
}
